/**
 * 表格格式化数据
 * @des {
 *     目前仅明细表使用
 * }
 * */
import { $each, $isEmptyArr, isNullOrEmpty } from '../index';
import { handleDetailTableConditions } from '../conditionFormat/useConditionFormat.js';
import { cloneDeep } from 'lodash-es';
export function tableFormat(datasets) {
    let sourceFields = [];
    let realData = []; // 原始数据
    let timeStampData = {}; // 时间类型数据
    let dimensionName = []; // 时间类型数据名称
    const { conditionOptions, fieldNameChainMap, type, collectdataSetType, source, dimensions, conditionData, sortInfo={} } = datasets;
    const { xAxis, yAxisProgress, yAxisCondition } = dimensions;
    // 判断是否是多列
    let xAxisArr = [];
    if (xAxis && xAxis.includes('&')) {
        xAxisArr = xAxis.split('&');
    } else {
        xAxisArr = [xAxis];
    }
    // 条件项存在多度量
    let yAxisConditionArr = [];
    if (yAxisCondition && yAxisCondition.includes('&')) {
        yAxisConditionArr = yAxisCondition.split('&');
    } else {
        yAxisConditionArr = [yAxisCondition];
    }
    // 排除条件项数据后的名称集合
    const dimensionsMap = [...xAxisArr, yAxisProgress]
    if (!source.length) {
        return {
            seriesData: [],
            //拖入字段，接口返回空数据，也显示表头数据
            columns:  isNullOrEmpty(xAxis) && isNullOrEmpty(yAxisProgress)  ? [] : dimensionsMap.map(item=> {
                return {
                        title: item,
                        dataIndex: item,
                        key: item,
                    }
            })
        }
    }
    let seriesData = [];
    let columns = [{
        title: '序号',
        dataIndex: 'key',
        key: 'key',
    }];
    // 静态数据就取source数据
    let sourceData = source;
    // 只包含维度的数据
    let realDimensionData = [];
    // 动态数据
    if (type !== 'STATIC') {
        sourceFields = {...fieldNameChainMap?.xAxis, ...fieldNameChainMap?.yAxisProgress };
        const typeNameList = Object.keys(collectdataSetType) && Object.keys(collectdataSetType).filter((type) => type === 'TIMESTAMP');
        dimensionName = typeNameList.map((name) => collectdataSetType[name]);
        // 获取时间类型的数据名称
        realData = cloneDeep(source);
        // 排除条件项的动态数据
        sourceData = source.map((data, index) => {
            let obj = {};
            dimensionsMap.map(name => {
                obj[name] = data[name]
            })
            return obj;
        })
        // 只包含维度的数据
        realDimensionData = source.map((data, index) => {
            let obj = {};
            xAxisArr.map(name => {
                obj[name] = data[name]
            })
            return obj;
        })
    }
    $each(sourceData, (item, index) => {
        seriesData.push({
            key: index,
            ...item
        })
    })
    let multiple = 0;
    $each(sourceData[0], (item, key) => {
        let column = {
            title: key,
            dataIndex: key,
            key: key,
        }
        if(!isNullOrEmpty(sortInfo[key])) {
            multiple ++;
            column.sorter= {
                multiple,
            }
            column.sortOrder = sortInfo[key];
        }
        columns.push(column);
    })
    // 查找时间类型数据
    dimensionName.forEach((name) => {
        const result = realData.map((item) => {
            return {value: item[name]};
        });
        timeStampData[name] = result;
    });
    // 条件项存在
    if(!$isEmptyArr(yAxisConditionArr)) {
        yAxisConditionArr.forEach((yItem) => {
            const yConditionData = [];
            source.forEach((item) => {
                yConditionData.push({
                    value: item[yItem],
                });
            });
            // 根据原始字段名称收集数据
            if(fieldNameChainMap['yAxisCondition'] && fieldNameChainMap['yAxisCondition'][yItem]) {
                conditionData[fieldNameChainMap['yAxisCondition'][yItem]].push(...yConditionData);
            }
        });
    }
    // 处理条件格式
    const componentAttrs = {
        conditionOptions,
        config: {
            sourceFields,
            data: seriesData,
            realData: source,
            conditionData,  // 条件项数据
        },
        timeStampData,
    };
    // 条件格式后的数据集合
    const tableConditionSetMap = handleDetailTableConditions(componentAttrs);
    return {
        seriesData,
        columns,
        tableConditionSetMap,
        realDimensionData,
    }
}
